Java进阶-泛型机制

  1. JDK5.0 之后推出的新特性:泛型
  2. 泛型这种语法机制,只在程序编译阶段起作用,只是给编译器参考的(运行阶段泛型没用)
  3. 使用了泛型的好处?
    • 集合中存储的元素类型统一了
    • 从集合中取出的元素类型是泛型指定的类型,不需要进行大量的“向下转型”
  4. 泛型缺点?
    • 导致集合中存储的元素缺乏多样性
    • 大多数业务中,集合中元素的类型还是统一的,所以这种泛型特性被大家认可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GenericTest01 {

public static void main(String[] args) {
// // 不使用泛型机制,分析程序存在缺点
// List myList = new ArrayList();
//
// // 准备对象
// Cat c = new Cat();
// Bird b = new Bird();
//
// // 将对象添加到集合中
// myList.add(c);
// myList.add(b);
//
// // 遍历集合,取出每个 Animal,执行 move
// Iterator it = myList.iterator();
// while (it.hasNext()) {
// // 没有这个语法,通过迭代器取出的就是 Object
// // Animal a = it.next();
//
// Object obj = it.next();
// // obj 中没有 move方法,无法调用,需要向下转型
// if (obj instanceof Animal) {
// Animal a = (Animal) obj;
// a.move();
// }
// }

// 使用 JDK5 之后的泛型机制
// 使用泛型 List<Animal> 之后,表示List集合只允许存储 Animal 类型的数据
// 用泛型来指定集合存储的数据类型
List<Animal> myList = new ArrayList<Animal>();

// 指定List集合中只能存储Animal,那么存储String就编译报错了
// 这样用了泛型之后,集合中元素的数据类型更加统一了
// myList.add("abc");

Cat c = new Cat();
Bird b = new Bird();
myList.add(c);
myList.add(b);

// 获取迭代器
// 这个表示迭代器迭代的是 Animal 类型
Iterator<Animal> it = myList.iterator();
while (it.hasNext()) {
// // 使用泛型之后,每一次迭代返回的数据都是 Animal 类型
// Animal a = it.next();
// // 这里不需要强制类型转换了,直接调用
// a.move();

// 调用子类型特有的方法还是需要向下转换的
Animal a = it.next();
if (a instanceof Cat) {
Cat x = (Cat) a;
x.catchMouse();
}
if (a instanceof Bird) {
Bird y = (Bird) a;
y.fly();
}
}

}

}

class Animal {
public void move() {
System.out.println("动物在移动!");
}
}

class Cat extends Animal {
public void catchMouse() {
System.out.println("猫抓老鼠!");
}
}

class Bird extends Animal {
public void fly() {
System.out.println("鸟儿在飞翔!");
}
}